package com.guanyong.webapi.controllers.api;

import com.guanyong.model.UserBaseModel;
import com.guanyong.services.VideoCountService;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.webapi.handle.UserHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by mxk on 2017/6/15.
 */


@RestController
@RequestMapping("/api/videocount/")
public class VideoCountApi {

    @Autowired
    VideoCountService videoCountService;
    @Autowired
    UserHandle userHandle;

    @RequestMapping(value = "addlooklog", method = RequestMethod.POST)
    public ResultObject<Integer> AddLookResut(HttpServletRequest request,
                                              Integer classtype,
                                              String objectid,
                                              Integer apptype) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            String ip = com.guanyong.utils.NetUtils.IPUtil.getIpAddr(request);
            resultObject = videoCountService.AddLookLog(classtype, userBaseModel.getfId(),
                    objectid, ip, apptype);
        } else {
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;

    }

    @RequestMapping(value = "addgoodlog", method = RequestMethod.POST)
    public ResultObject<Integer> AddGoodResut(HttpServletRequest request,
                                              Integer classtype,
                                              String objectid,
                                              Integer apptype) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            String ip = com.guanyong.utils.NetUtils.IPUtil.getIpAddr(request);
            resultObject= videoCountService.AddGoodLog(classtype, userBaseModel.getfId(),
                    objectid, ip,
                    apptype);
        } else {
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;

    }

    //添加收藏
    @RequestMapping(value = "addcollectlog", method = RequestMethod.POST)
    public ResultObject<Integer> AddCollectResut(HttpServletRequest request,
                                                 Integer classtype,
                                                 String objectid,
                                                 Integer apptype) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            //2017年9月4日 09:03:00 mxk 修改判断 参数是否为Null
            if (objectid!=null && objectid.trim().length()==24){
                String ip = com.guanyong.utils.NetUtils.IPUtil.getIpAddr(request);
                resultObject= videoCountService.AddCollectLog(classtype, userBaseModel.getfId(),
                        objectid, ip, apptype);
            }
            else{
                resultObject.setCode(ResultCode.ParamsError);
                resultObject.setErrorMsg("参数错误！参数objectid="+objectid);
            }

        } else {
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;

    }

    //取消收藏（多个）
    @RequestMapping(value = "delcollectlogidlist", method = RequestMethod.POST)
    public ResultObject<Integer> DelCollectIdListResut(HttpServletRequest request,
                                                       String  fidlist) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            resultObject= videoCountService.DelCollectLogIdList(fidlist);
        } else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;

    }


    //取消收藏
    @RequestMapping(value = "delcollectlog", method = RequestMethod.POST)
    public ResultObject<Integer> DelCollectResut(HttpServletRequest request,
                                                 Integer id) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            resultObject= videoCountService.DelCollectLog(id);
        } else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;

    }
    //取消收藏
    @RequestMapping(value = "delcollectlogByObjectid", method = RequestMethod.POST)
    public ResultObject<Integer> DelCollectByObjectid(HttpServletRequest request,
                                                      String objectid) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            resultObject= videoCountService.DelCollectLogByObjectid(userBaseModel.getfId(),objectid);
        } else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;

    }





    //获取我的收藏
    @RequestMapping(value = "getmycollectlog", method = RequestMethod.POST)
    public ResultObject<HashMap<String, Object>> GetMyCollectlog(HttpServletRequest request,
                                                                 @RequestParam int pno,
                                                                 @RequestParam int pagesize,
                                                                 @RequestParam int allcount) {
        ResultObject<HashMap<String, Object>> resultObject = new ResultObject<HashMap<String,
            Object>>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            HashMap<String, Object> objectHashMap = videoCountService.GetCollectList(
                    userBaseModel.getfId(), pno,
                    pagesize, allcount);

            if (objectHashMap != null) {
                resultObject.setResult(objectHashMap);
            }
        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;
    }


    @RequestMapping(value = "addsharelog", method = RequestMethod.POST)
    public ResultObject<Integer> AddShareResut(HttpServletRequest request,
                                               Integer classtype,
                                               String objectid,
                                               Integer apptype) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            String ip = com.guanyong.utils.NetUtils.IPUtil.getIpAddr(request);
            resultObject= videoCountService.AddShareLog(classtype, userBaseModel.getfId(),
                    objectid, ip, apptype);
        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return  resultObject;
    }

    @RequestMapping(value = "addplaylog", method = RequestMethod.POST)
    public ResultObject<Integer> AddPlayResut(HttpServletRequest request,
                                              @RequestParam String phoneSN,
                                              Integer classtype,
                                              String objectid,
                                              Integer apptype,
                                              Integer playtime) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        String ip = com.guanyong.utils.NetUtils.IPUtil.getIpAddr(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            if (objectid!=null&&classtype!=null){
                resultObject= videoCountService.AddPlayLog(classtype, userBaseModel.getfId(),phoneSN, objectid, ip,
                        apptype,
                        playtime);
            }else{
                resultObject.setCode(ResultCode.OperateFail);
            }

        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;
    }




    @RequestMapping(value = "delplaylog", method = RequestMethod.POST)
    public ResultObject<Integer> DelPlayResut(HttpServletRequest request,
                                              Integer fid) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            resultObject= videoCountService.DelPlayLog(fid);
        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;
    }

    @RequestMapping(value = "delplaylogidlist", method = RequestMethod.POST)
    public ResultObject<Integer> DelPlayResut(HttpServletRequest request, String  fidlist) {
        ResultObject<Integer> resultObject = new ResultObject<Integer>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            resultObject= videoCountService.DelPlayLogIdlist(userBaseModel.getfId(),fidlist);
        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;
    }


    //获取我的浏览记录
    @RequestMapping(value = "getmyplaylog", method = RequestMethod.GET)
    public ResultObject<HashMap<String, Object>> GetMyPlaylog(HttpServletRequest request,
                                                              @RequestParam String phoneSN,
                                                              @RequestParam Integer isweek,
                                                              @RequestParam int offset,
                                                              @RequestParam int limit,
                                                              @RequestParam int allcount) {
        ResultObject<HashMap<String, Object>> resultObject = new ResultObject<HashMap<String,
                Object>>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        if (userBaseModel != null && !userBaseModel.getfId().equals("")) {
            HashMap<String, Object> objectHashMap = videoCountService.GetPlayList(userBaseModel.getfId(),phoneSN,
                    isweek,
                    offset,
                    limit, allcount);

            if (objectHashMap != null) {
                resultObject.setResult(objectHashMap);
            }
        }
        else{
//            HashMap<String, Object> objectHashMap = videoCountService.GetPlayList(null,phoneSN,
//                    isweek,
//                    offset,
//                    limit, allcount);
//            if (objectHashMap != null) {
//                resultObject.setResult(objectHashMap);
//            }
            resultObject.setCode(ResultCode.NoLogin);
        }
        return resultObject;
    }

    //获取我的浏览记录
    @RequestMapping(value ="getmyplaylogios", method = RequestMethod.GET)
    public ResultObject<HashMap<String, Object>> GetMyPlaylogIos(HttpServletRequest request,
                                                              @RequestParam String phoneSN,
                                                              @RequestParam int pno,
                                                              @RequestParam int pagesize,
                                                              @RequestParam int allcount) {
        ResultObject<HashMap<String, Object>> resultObject = new ResultObject<HashMap<String,
                Object>>();
        UserBaseModel userBaseModel = userHandle.getCurUser(request);
        String userid="";
        if (userBaseModel!=null && userBaseModel.getfId()!=""){
            userid=userBaseModel.getfId();
            HashMap<String, Object> objectHashMap = videoCountService.GetPlayListLog(userid,phoneSN,
                    pno,
                    pagesize, allcount);

            if (objectHashMap != null) {
                resultObject.setResult(objectHashMap);
            }
        }
        else{
            resultObject.setCode(ResultCode.NoLogin);
        }

        return resultObject;
    }


//
//    @RequestMapping(value = "addbuylog", method = RequestMethod.POST)
//    public ResultObject<Integer> AddBuyResut(HttpServletRequest request,
//                                              Integer classtype,
//                                              String objectid,
//                                              Integer apptype){
//        String userid="";
//        String ip=com.guanyong.utils.NetUtils.IPUtil.getIpAddr(request);
//        return videoCountService.AddBuyLog(classtype,userid,objectid,ip,apptype);
//
//    }


}
